package org.zttc.itat.action;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zttc.itat.model.News;
import org.zttc.itat.model.Pager;
import org.zttc.itat.model.Sort;
import org.zttc.itat.model.SortType;
import org.zttc.itat.model.SystemContext;
import org.zttc.itat.service.INewsService;
import org.zttc.itat.service.ISortService;
import org.zttc.itat.service.ISortTypeService;
import org.zttc.itat.service.IUserService;
import org.zttc.itat.service.SortTypeService;
import org.zttc.itat.util.ActionUtil;
import org.zttc.itat.util.FileUtil;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("newsAction")
@Scope("prototype")
public class NewsAction extends ActionSupport implements ModelDriven<News>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private News news;
	private int sortId;
	private int sortTypeId;
	private int userId;
    private	List<SortType> list;  
    //上传文件存放路径   
    private final static String UPLOADDIR = "/images";   
    
    private File picture;
    private String pictureFileName;
	@Resource(name="newsService")
    private INewsService newsService;
	@Resource(name="userService")
	private IUserService userService;
	@Resource(name="sortService")
	private ISortService sortService;
	@Resource(name="sortTypeService")
	private ISortTypeService sortTypeService;
	
	@Override
	public News getModel() {
    if(news==null) news = new News();
		return news;
	}
	
	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public int getSortId() {
		return sortId;
	}
	public void setSortId(int sortId) {
		this.sortId = sortId;
	}
    
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}
	
	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}
    public List<SortType> getList() {
		return list;
	}
    
	public int getSortTypeId() {
		return sortTypeId;
	}

	public void setSortTypeId(int sortTypeId) {
		this.sortTypeId = sortTypeId;
	}

	public void setList(List<SortType> list) {
		this.list = list;
	}
	public String add() throws Exception{
	    if(sortId<=0) this.addFieldError("sortId","一级栏目不能为空");
	    if(this.hasActionErrors()) addInput();
	    String path =null;
	    if(picture!=null){
	    path = FileUtil.upload(picture, pictureFileName);
	    path = SystemContext.getRealPath()+path;
	    }
	    news.setImg(path);
	    news.setCreateDate(new Date());
	    System.out.println(sortTypeId);
	    if(sortTypeId<=0){
	       newsService.add(news, sortId,SystemContext.getLoginUser());
	    }else{
	    	newsService.add(news, sortId, sortTypeId, SystemContext.getLoginUser());
	    }
	    ActionUtil.setUrl("later_news_list.action");
		return ActionUtil.REDIRECT;
	}
	public String objectToJson(){  
	     list = sortService.listScopeSort(sortId); 
        return "json";
	}
	public String addInput(){
		List<Sort> ds = sortService.listAllSort();
		ActionContext.getContext().put("ds", ds);
		return SUCCESS;
	}
	
	public String list(){
		Pager<News> pages = newsService.find();
		ActionContext.getContext().put("pages", pages);
		List<Sort> ds = sortService.listAllSort();
		ActionContext.getContext().put("ds", ds);
		return SUCCESS;
	}
	
	public String update() throws Exception{
		 News n = newsService.load(news.getId());
		 if(sortId<=0) this.addFieldError("sortId","一级栏目不能为空");
		 if(this.hasActionErrors()) addInput();
		 String path =null;
		 if(picture!=null){
		    path = FileUtil.upload(picture, pictureFileName);
		    path = SystemContext.getRealPath()+path;
		 }
		 n.setImg(path);
		 n.setCreateDate(new Date());
		 n.setContent(news.getContent());
		 n.setSort(sortService.load(sortId));
		 if(sortTypeId>=0){
			 n.setSortType(sortTypeService.load(sortTypeId));
		 }
		 newsService.update(news);
		 ActionUtil.setUrl("news_post.action");
	     return ActionUtil.REDIRECT;
	}
	
	public String updateInput(){
		news = newsService.load(news.getId());
		ActionContext.getContext().put("ds",sortService.listAllSort());
		ActionContext.getContext().put("dts",sortService.listScopeSort(sortId));
		return SUCCESS;
	}
	public String show(){
		if(sortId<=0){
			news = newsService.load(sortTypeId);
		}else{
			news = newsService.load(sortId);
		}
		return SUCCESS;
	}
	
	public String delete(){
       newsService.delete(news.getId());
       ActionUtil.setUrl("/later_user_post.action");
	   return ActionUtil.REDIRECT;
	}
}
